package utils;

import android.annotation.SuppressLint;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/12/6 0006.
 */

public class StringUtil {

    /**
     * 通过多个以某个String间隔的多个String组成的String解析出String的list
     *
     * @param str         原始String
     * @param strInterval 间隔的字符串
     * @return
     */
    public static List<String> getStringListByString(String str,
                                                     String strInterval) {
        List<String> listUrls = new ArrayList<String>();
        if (TextUtils.isEmpty(str)) {
            return listUrls;
        }

        try {

            while (str.indexOf(strInterval) != -1) {
                String s = str.substring(0, str.indexOf(strInterval));
                if (!TextUtils.isEmpty(s)) {
                    listUrls.add(s);
                }

                str = str.substring(
                        str.indexOf(strInterval) + strInterval.length(),
                        str.length());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<String>();
        }

        listUrls.add(str);// 加入最末尾的一个url

        return listUrls;
    }

    public static String listToString(List<String> list, char separator) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            sb.append(!TextUtils.isEmpty(list.get(i)) ? list.get(i) : "");
            if (i < list.size() - 1) {
                sb.append(separator);
            }
        }
        return sb.toString();
    }

    //格式化时间
    public static String formatTime(String time) {
        if (TextUtils.isEmpty(time))
            return "";
        time = time.replaceAll("/", "-");
        if (time.length() > 16) {
            return time.replace("T", " ").substring(0, 16);
        }
        return time.replace("T", " ");
    }

    //格式化时间
    public static String formatTime(Date time) {
        if (time == null)
            return "";
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        return sd.format(time);
    }

    //String 转Date
    public static Date getDate(String text) {
        text = text.replaceAll("/", "-");
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return sd.parse(text);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取外置SD卡路径 (没有SD卡，就获取内置存储路径)
     *
     * @return
     */
    @SuppressLint("LongLogTag")
    public static String getSDCardPath() {
        String cmd = "cat /proc/mounts";
        Runtime run = Runtime.getRuntime();// 返回与当前 Java 应用程序相关的运行时对象
        try {
            Process p = run.exec(cmd);// 启动另一个进程来执行命令
            BufferedInputStream in = new BufferedInputStream(p.getInputStream());
            BufferedReader inBr = new BufferedReader(new InputStreamReader(in));

            String lineStr;
            while ((lineStr = inBr.readLine()) != null) {
                // 获得命令执行后在控制台的输出信息
                Log.i("CommonUtil:getSDCardPath", lineStr);
                if (lineStr.contains("sdcard")
                        && lineStr.contains(".android_secure")) {
                    String[] strArray = lineStr.split(" ");
                    if (strArray != null && strArray.length >= 5) {
                        String result = strArray[1].replace("/.android_secure",
                                "");
                        return result;
                    }
                }
                // 检查命令是否执行失败。
                if (p.waitFor() != 0 && p.exitValue() == 1) {
                    // p.exitValue()==0表示正常结束，1：非正常结束
                    Log.e("CommonUtil:getSDCardPath", "命令执行失败!");
                }
            }
            inBr.close();
            in.close();
        } catch (Exception e) {

            return Environment.getExternalStorageDirectory().getPath();
        }
        return Environment.getExternalStorageDirectory().getPath();
    }

    public static boolean isEmpty(String s) {

        return TextUtils.isEmpty(s);
    }

    public static Date getYYYYMMDD(String s) {

        Date date = null;
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");

        try {
            date = sd.parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
        } finally {
            return date;
        }


    }
}
